package cc.core.pullrefresh.extra;

import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.view.ViewHelper;

import cc.core.pullrefresh.R;

import android.R.attr;
import android.R.raw;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * cc.core.pullrefresh.extra.HeaderLayout
 *
 * @author YuanChao <br/>
 *         create at 2015年6月22日 下午1:43:43
 */
public class HeaderLayout extends BaseHeaderLayout {
    private static final String TAG = "HeaderLayout";
    private TextView refreshTitle;
    private ImageView headerImg;
    private int textColor;
    private float textSize = 14;
    private int imgSrc;
    private int pullValue;
    private String refreshingText, pullrefreshText, releaseText;
    private boolean isCustomz;

    public HeaderLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public int getViewById() {
        return 0;
    }

    @Override
    public void initView(Context context, AttributeSet attrs) {

        int resid = getViewById();
        if (resid == 0) {
            LayoutInflater.from(context).inflate(R.layout.layout_header, this);
        } else {
            isCustomz = true;
            LayoutInflater.from(context).inflate(resid, this);
        }

        refreshTitle = (TextView) findViewById(R.id.pr_title);

        headerImg = (ImageView) findViewById(R.id.header_img);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        if (attrs != null) {
            TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.PullRefresh);
            textColor = ta.getColor(R.styleable.PullRefresh_header_textColor, Color.parseColor("#ff757575"));
            textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, dm);
            textSize = ta.getDimensionPixelSize(R.styleable.PullRefresh_header_textSize, (int) textSize);// Ĭ�������С14sp
            imgSrc = ta.getResourceId(R.styleable.PullRefresh_header_img, R.drawable.ic_pr_header_pg);
            ta.recycle();
            refreshTitle.setTextColor(textColor);
            refreshTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            headerImg.setImageResource(imgSrc);
        }

        initDefAnim(headerImg);

    }

    @Override
    public void updateTitle() {
    }

    /**
     * setReleaseToRefresh: 设置正在刷新动作文字. <br/>
     *
     * @author cc
     */
    public void setRefreshing() {
        if (TextUtils.isEmpty(refreshingText))
            refreshTitle.setText(getResources().getString(R.string.text_refreshing));
        else
            refreshTitle.setText(refreshingText);

        rotation.start();
    }

    @Override
    public void onHeaderRefreshing(boolean isRefreshing) {
        if (isRefreshing) {
            if (!rotation.isRunning()) {
                rotation.start();
            }
        } else {
            rotation.cancel();
        }

    }

    /**
     * setReleaseToRefresh: 设置下拉刷新动作文字. <br/>
     *
     * @author cc
     */
    public void setPullToRefresh() {
        rotation.cancel();
        if (TextUtils.isEmpty(pullrefreshText))
            refreshTitle.setText(getResources().getString(R.string.text_pullrefresh));
        else
            refreshTitle.setText(pullrefreshText);
    }

    /**
     * setReleaseToRefresh: 设置释放刷新动作文字. <br/>
     *
     * @author cc
     */
    public void setReleaseToRefresh() {
        rotation.cancel();
        if (TextUtils.isEmpty(releaseText))
            refreshTitle.setText(getResources().getString(R.string.text_release_refresh));
        else
            refreshTitle.setText(releaseText);
    }

    @Override
    public void setAnimValue(float value) {
        rotation.cancel();
        ViewHelper.setRotation(headerImg, value);
    }

    @Override
    public void setPullValue(int value) {
        this.pullValue = value;
    }

    @Override
    public void setError(boolean isError) {

    }

    public void setRefreshTitle(TextView refreshTitle) {
        this.refreshTitle = refreshTitle;
    }

    public void setRefreshingText(String refreshingText) {
        this.refreshingText = refreshingText;
    }

    public void setReleaseText(String releaseText) {
        this.releaseText = releaseText;
    }

}
